/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.world;

import cz.muni.fi.diplomka.servicelayer.dto.Tile;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.WorldManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.WorldManagerImpl;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.Collections;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.resource.ContextRelativeResource;

/**
 *
 * @author Jakub Kutil
 */
public class WorldPage extends WebPage {

    private WorldManager worldManager = new WorldManagerImpl();

    public WorldPage(final PageParameters parameters) {

        add(new FeedbackPanel("feedback"));

        final Long id;
        final int x;
        final int y;

        if (parameters.containsKey("id")) {
            PageParameters p = new PageParameters();
            id = parameters.getAsLong("id");
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));

            if (parameters.containsKey("x_world")) {
                x = parameters.getAsInteger("x_world");

            } else {
                x = worldManager.getUpperLeftCorner().getX_coordinate();
            }

            if (parameters.containsKey("y_world")) {
                y = parameters.getAsInteger("y_world");
            } else {
                y = worldManager.getUpperLeftCorner().getY_coordinate();
            }

            final TextField<String> xField = new TextField<>("xField", Model.of(""));

            final TextField<String> yField = new TextField<>("yField", Model.of(""));

            Form<?> goTo = new Form<Void>("goTo") {
                @Override
                protected void onSubmit() {
                    try {

                        PageParameters pageParameters = new PageParameters();
                        pageParameters.add("id", id.toString());
                        pageParameters.add("x_world", xField.getValue());
                        pageParameters.add("y_world", yField.getValue());
                        setResponsePage(WorldPage.class, pageParameters);
                    } catch (NumberFormatException ex) {
                        error("X and Y have to be numbers.");
                    }
                }
            };

            goTo.add(xField);
            goTo.add(yField);
            add(goTo);

            List<Tile> squareOfWorld = worldManager.getSquareOfWorld(x, y);
            Collections.sort(squareOfWorld);

            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {

                    if (i == 0) {
                        add(new Label("x" + (j + 1), squareOfWorld.get(i * 10 + j).getCoordinates().getX_coordinate() + ""));
                    }
                    if (j == 0) {
                        add(new Label("y" + (i + 1), squareOfWorld.get(i * 10 + j).getCoordinates().getY_coordinate() + ""));
                    }

                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("x", String.valueOf(squareOfWorld.get(i * 10 + j).getCoordinates().getX_coordinate()));
                    pageParameters.add("y", String.valueOf(squareOfWorld.get(i * 10 + j).getCoordinates().getY_coordinate()));

                    BookmarkablePageLink<Void> link = new BookmarkablePageLink<>("x" + (j + 1) + "y" + (i + 1), TilePage.class, pageParameters);

                    link.add(new Image("id", new ContextRelativeResource("/images/world/" + squareOfWorld.get(i * 10 + j).getType() + ".png")));
                    add(link);
                }
            }
        }
    }
}
